using System.Collections.Generic;
using System.Threading.Tasks;
using Sustainalytics.Synchronization.Validation.Entities.CorporateGovernance;
using Sustainalytics.Synchronization.Validation.Internals;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;

namespace Sustainalytics.Synchronization.Validation.Validators
{
    public class CorporateGovernanceValidator : Validator<GovernanceValidationProfile>
    {
        public CorporateGovernanceValidator(IEnumerable<Rule<GovernanceValidationProfile>> rules)
            : base(rules)
        {
        }

        public override Task<ValidationResult> ValidateAsync(GovernanceValidationProfile validatedObject, GovernanceValidationProfile referenceObject)
        {
            ValidationResult = new ValidationResult();

            if (validatedObject.Equals(default(GovernanceValidationProfile)))
            {
                throw new ValidationException("Invalid object");
            }

            foreach (var validationRule in ValidationRules)
            {
                if (!validationRule.IsValid(validatedObject, referenceObject))
                {
                    ValidationResult.AddError(
                        validationRule.GetKey(validatedObject),
                        validationRule.GetErrorMessage(), 
                        validationRule.Severity,
                        validationRule.BusinessResearchProduct,
                        validationRule.Code,
                        validationRule.InformationalMessage,
                        validationRule.Triggers);
                }
            }

            return Task.FromResult(ValidationResult);
        }
    }
}